<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Banco Imagens</title>
</head>

<body bgcolor="#FFFFFF">
<?php
require('funcoes.inc.php');

$link = conecta();

if( $_SERVER['REQUEST_METHOD'] == 'POST' ){
	if( isset($_POST['apagar']) ){
		unset($_POST['apagar']);
		foreach( $_POST as $id => $value )
			mysql_query("DELETE FROM imagem WHERE imagem_id='".$id."'",$link);
	}
	else {
		$extensoes = array('image/jpeg','image/jpg','image/pjpeg');
		if( in_array($_FILES['foto']['type'],$extensoes) ){
			// Local para salvar a Imagem.
			$destination_path = '..'.DIRECTORY_SEPARATOR.'bancoImgs'.DIRECTORY_SEPARATOR;
	
			do{
				$name = md5($_FILES['foto']['name'].time().rand());
				$num = mysql_num_rows(mysql_query("SELECT valor FROM imagem WHERE valor='".$name."'"));
			}while( $num != 0 );
			mysql_query("INSERT INTO imagem ( nome , valor ) VALUES ('".$_FILES['foto']['name']."','".$name.".jpg')",$link);
			
			move_uploaded_file($_FILES['foto']['tmp_name'], $destination_path.$name.'.jpg');
		}
		else
			$error = "Formato Incorreto.";		
	}
}

$qimagem = mysql_query("SELECT nome,valor,imagem_id FROM imagem ORDER BY imagem_id DESC",$link);

mysql_close($link);

?>
<style>
td:hover{
	background-color:#EEE;
}
a{
	text-decoration: none;
}
</style>
<BR />
<center>
<form action="" method="post" ENCTYPE="multipart/form-data" >
<input name="foto" type="file" size="10"  /><BR /><BR />
<input type="submit" value="Enviar Foto" />
</form>
</center>
<BR />
<form action="" method="post">
<input type="hidden" name="apagar" value="true"/>
<input type="submit" value="Apagar" />
<table width="100%" border="0">
<?php
	$num = mysql_num_rows($qimagem);
	for($i=0;$row = mysql_fetch_row($qimagem);$i++,$num--){
		if( $i == 0 )
			echo '<TR>'."\n";
		echo '<TD ALIGN="CENTER">
			  <input type="checkbox" name="'.$row[2].'" value="true">
			  <a href="../bancoImgs/'.$row[1].'" target="_new">
			  <IMG SRC="../bancoImgs/'.$row[1].'" ALT="'.$row[0].'" width="80" heigth="80" border="0"></a>
			  </TD>'."\n";
		if( $i == 4 || $num == 1){
			echo '</TR>'."\n\n";
			$i=-1;
		}
	}
?>
</table>
</form>
</body>
</html>
